<template >
  <wrapper need-bottom-tab style="width: 100%;">
    <home-tab ref="homeTab" v-if="currentIndex === 0" />
    <recruitmentTab
      ref="homePage"
      v-if="settings.id!=='csxr' &&currentIndex === 1&&userStatus!==4"
    />
    <mine-tab
      v-if="showMineTab"
    />
    <bottom-tab :tabs="tabs" need-indicator @change="onTabChange" />
  </wrapper>
</template>
<script>
import { verify_home } from "@/utils/public";
import HomeTab from "./HomeTab";
import recruitmentTab from "../../../src/pages/pushPages/home/index";
// import recruitmentTab from "./HomeTab";
import MineTab from "./MineTab";
import Wrapper from "@/components/Wrapper";
import BottomTab from "@/components/BottomTab";
import { mapState } from "vuex";
import { subjectInfo } from "@/settings";
import { loadEmpInfoReq } from "@/services/auth";

const SHARE_NAME = process.env.VUE_APP_SHARE_NAME

export default {
  components: {
    Wrapper,
    BottomTab,
    HomeTab,
    MineTab,
    recruitmentTab
  },
  data() {
    return {
      currentIndex: 0,
      fail: false,
      tabs: []
    };
  },
  computed: {
    ...mapState(["user", "site", "settings"]),
    userStatus() {
      return this.user.userStatus;
    },
    no_emp() {
      return (
        !!this.user.userId &&
        (this.site.currentSite ? !this.site.employeeId : false)
      );
    },
    showMineTab() {
      if (this.settings.id==='csxr' && this.currentIndex === 1) {
        return true;
      } else if (this.currentIndex === 2) {
        return true;
      } else if (this.userStatus === 4 && this.currentIndex === 1) {
        return true;
      } else {
        return false;
      }
    }
  },
  watch: {
    userStatus: {
      handler() {
        this.updateTabs()
      },
      immediate: true
    },
  },
  created() {
    verify_home();
    console.log(this.settings.id, this.currentIndex, this.userStatus);
    uni.setNavigationBarColor({
      frontColor: "#ffffff",
      backgroundColor: this.settings.theme
    });
  },
  onLoad: function(option) {
    if (option.redirect) {
      let redirect = JSON.parse(option.redirect);
      let query = redirect.query;
      let data = [];
      Object.keys(query).map(i => {
        let text = `${i}=${query[i]}`;
        data.push(text);
      });
      uni.redirectTo({
        url: `${redirect.url}?${data + ""}`
      });
    }
  },
  onShareAppMessage() {
    return {
      title: SHARE_NAME,
      path: "/pages/index/index"
    };
  },
  onShareTimeline() {
    return {
      title: SHARE_NAME,
      path: "/pages/index/index"
    };
  },
  onShow: function() {
    wx.hideHomeButton();
    if (this.$refs.homeTab) {
      this.$refs.homeTab.init();
    }
    if (this.showMineTab) {
      loadEmpInfoReq()
        .then(res => {
          console.log('mine success');
          this.$store.commit("user/updateUserInfo");
          this.$store.commit("site/updateCompanyInfo");
          if (res) {
            uni.setStorageSync("siteId", res.siteId);
            uni.setStorageSync("idCard", res.idCard);
            uni.setStorageSync("idName", res.name);
            uni.setStorageSync("employeeId", res.id);
            uni.setStorageSync("employeeName", res.name);
            uni.setStorageSync("companyId", res.companyId);
            uni.setStorageSync("companyName", res.companyName);
            uni.setStorageSync("verified", res.verified);
            uni.setStorageSync("wgkInfo", res.vxWgkUserAuthResultVO);
            uni.setStorageSync("wgk_verified", res.vxWgkUserAuthResultVO && res.vxWgkUserAuthResultVO.authorize_state === "AUTHORIZED");
            uni.setStorageSync("userStatus", res.status);
            console.log('userStatus2', res, uni.getStorageSync('userStatus'))
            this.$store.commit("user/updateUserInfo");
            this.$store.commit("site/updateCompanyInfo");
          }
        })
        .catch((err) => {
          console.log('mine fail', err)
        })
    }
  },
  methods: {
    updateTabs() {
      if (this.settings.id === "csxr" || this.userStatus === 4) {
        this.tabs = [
          {
            title: "首页",
            active_icon: subjectInfo.icon_shouye,
            inactive_icon: "/static/images/tabIcon/shouye_inactive.png"
          },
          {
            title: "我的",
            active_icon: subjectInfo.icon_wode,
            inactive_icon: "/static/images/tabIcon/wode_inactive.png"
          }
        ];
      } else {
        this.tabs = [
          {
            title: "首页",
            active_icon: subjectInfo.icon_shouye,
            inactive_icon: "/static/images/tabIcon/shouye_inactive.png"
          },
          {
            title: "内推招聘",
            active_icon: subjectInfo.icon_mall,
            inactive_icon: "/static/images/tabIcon/weiRecruitment.png"
          },
          {
            title: "我的",
            active_icon: subjectInfo.icon_wode,
            inactive_icon: "/static/images/tabIcon/wode_inactive.png"
          }
        ];
      }
    },
    onReachBottom() {
      if (this.$refs.homePage) {
        this.$refs.homePage.onReach();
      }
    },
    goSwitch() {
      uni.navigateTo({
        url: "/pages/setting/site"
      });
    },
    onTabChange(index) {
      // verify_home();
      this.currentIndex = index;
      if(this.settings.id!=='csxr'){
        if (index === 0) {
          uni.setNavigationBarTitle({
            title: "首页"
          });
        } else if (index === 1) {
          uni.setNavigationBarTitle({
            title: "招聘"
          });
        } else if (index === 2) {
          uni.setNavigationBarTitle({
            title: "我的"
          });
          // uni.navigateToMiniProgram({
          //   appId: "wx8831b0d2abce4652",
          //   success(res) {
          //     // 打开成功
          //   },
          //   fail(res) {
          //     uni.showToast({
          //       title: "商城打开失败",
          //       icon: "none",
          //       duration: 2000
          //     });
          //   }
          // });
        }
      }else{
        if (index === 0) {
          uni.setNavigationBarTitle({
            title: "首页"
          });
        } else if (index === 1) {
          uni.setNavigationBarTitle({
            title: "我的"
          });
        }
      }
    }
  }
};
</script>
<style lang="scss">
page {
  background: #fafbfb;
}
</style>
